﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Model;

namespace DB
{
    public class DbCustomer : DB.IDbCustomer
    {
            private DbConnection con;
        private const string DB_NAME = "Customer_xp"; //tjek
        
        public DbCustomer()
        {
            con = DbConnection.GetInstance();
        }
        //public int getNextID() // til at hjælpe, skal ikke bruges under movie
        //{

        //    String query = null;
        //    int nextInt = 1;
        //    try
        //    {
        //        query = "select MAX(orderID) from " + DB_NAME;
        //        SqlCommand command = con.GetDbCommand(query);
        //        nextInt = (Int32)command.ExecuteScalar();
        //        nextInt++;
        //        con.CloseConnection();
        //    }
        //    catch (Exception e)
        //    {
        //    }
        //    return nextInt;

        //}

        public int InsertCustomer(Customer customer)
        {
            String query = null;
            int rc = -1;
            try
            {
                query = @"INSERT INTO Customer_xp VALUES (@phoneNo,@name, @address,@email, @type)";
                //Console.WriteLine(query);

                //SqlCommand command = con.GetDbCommand(query);
                SqlCommand command = con.GetDbCommand(query);
                command.Parameters.AddWithValue("@phoneNo", customer.PhoneNo);
                command.Parameters.AddWithValue("@name", customer.Name);
                command.Parameters.AddWithValue("@address", customer.Address);
                command.Parameters.AddWithValue("@email", customer.Email);
                command.Parameters.AddWithValue("@type", customer.Type);
                rc = command.ExecuteNonQuery();
            }
            catch (SqlException sqle)
            {
                Console.WriteLine("SQLFejl i dblayer(insertcustomer): " + sqle.Message);
            }
            catch (Exception e)
            {
                Console.WriteLine("AlmFejl i dblayer(insertcustomer): " + e.Message);
            }
            finally
            {
                con.CloseConnection();
            }
            return rc;


        }
        public int DeleteCustomer(Customer customer)
        {
            String query = null;
            int rc = -1;
            try
            {
                query = @"DELETE FROM Customer_xp WHERE phoneNo = @phoneNo";
                //Console.WriteLine(query);
                SqlCommand command = con.GetDbCommand(query);
                command.Parameters.AddWithValue("@phoneNo", customer.PhoneNo);
                rc = command.ExecuteNonQuery();
            }
            catch (SqlException sqle)
            {
                Console.WriteLine(sqle.Message);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                con.CloseConnection();
            }
            return rc;
        }
        public void updateCustomer(Customer customer)
        {
        }

        public Customer FindCustomer(string phoneNo)
        {
            String query = null;
            Customer newCustomer = null;

            try
            {
                query = "select * from " + DB_NAME + " Where phoneNo = " + phoneNo;
                SqlCommand command = con.GetDbCommand(query);
                SqlDataReader data = command.ExecuteReader();
                while (data.Read())
                {

                    newCustomer = (BuildCustomer(data));

                }
                data.Close();
                con.CloseConnection();
            }

            catch (Exception e)
            {
            }
            return newCustomer;
        }

        public List<Customer> FindAllCustomers()
        {
            String query = null;
            List<Customer> customers = new List<Customer>();
            SqlDataReader reader = null;
            try
            {

                Customer newCustomer = new Customer();
                query = @"SELECT * FROM Customer_xp";
                //Console.WriteLine(query);
                SqlCommand command = con.GetDbCommand(query);
                reader = command.ExecuteReader();
                while(reader.Read())
                {

                    customers.Add(BuildCustomer(reader));

                }
                
                
            }
            catch (SqlException sqle)
            {
                Console.WriteLine(sqle.Message);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                
            }
            con.CloseConnection();
            reader.Close();
            return customers;
        }

        private Customer BuildCustomer(SqlDataReader dbReader)
        {
            Customer customerObj = new Customer();

            try
            {
                customerObj.Name = (String)dbReader["name"];
                customerObj.PhoneNo = (String)dbReader["phoneNo"];

                customerObj.Address = (String)dbReader["address"];
                
                customerObj.Email = (String)dbReader["email"];
                customerObj.Type = (String)dbReader["type"];
}
            catch(Exception e)
            {
                Console.WriteLine("Error in buiding customerobj");
                Console.WriteLine(e.Message);
            }

            return customerObj;
        }
    
    }
}
